#ifndef _SPI_H_
#define _SPI_H_

#include "../../Component.h"
#include<avr/io.h>
#include<avr/interrupt.h>
#include "../../../bit/bit.h"

namespace Hardware
{
namespace Avr
{

	typedef struct SPI_CONFIG
	{

		unsigned char interrupt : 1;
		unsigned char enable : 1;
		unsigned char order : 1;
		unsigned char select : 1;
		unsigned char polarity : 1;
		unsigned char phase : 1;
		unsigned char clockSelect : 3;
		
	} SpiConfig;

	typedef struct SPI_DATA
	{
		unsigned char data;
	} SpiData;

	class Spi : public Component<SpiConfig, SpiData>
	{
		public :

		enum ClockSelectPrescaler
		{
			Prescale4 = 0x00,
			Prescale16 = 0x01,
			Prescale64 = 0x02,
			Prescale128 = 0x03,
			Prescale2 = 0x04,
			Prescale8 = 0x05,
			Prescale32 = 0x06,
			Prescale64_2 = 0x07
		};
		Spi(){}
		Spi(SpiConfig c){configuration=c;}
		void config()
		{
			//set interrupt
			SPCR = SetBit(SPCR, SPIE, configuration.interrupt);
			//set enable
			SPCR = SetBit(SPCR, SPE, configuration.enable);
			//set data order
			SPCR = SetBit(SPCR, DORD, configuration.order);
			//set whether master or slave
			SPCR = SetBit(SPCR, MSTR, configuration.select);
			//set polarity
			SPCR = SetBit(SPCR, CPOL, configuration.polarity);
			//set phase
			SPCR = SetBit(SPCR, CPHA, configuration.phase);
			//set the clock rate
			SPCR = SetBitGroup(SPCR, 2, SPR0, 
				(configuration.clockSelect & 3)); 
			SPSR = SetBit(SPSR, SPI2X, (configuration.clockSelect>>2));
			
		}
		void run(){}
	};
}
}




#endif
